FileUploader = require "./../../../common/file-uploader/index.coffee"

$ () ->
	imgKeyValue1 = null

	new FileUploader {
        click_button: "upload1_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img1",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload1',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper1',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue1 = info.key
	}
	imgKeyValue2 = null

	new FileUploader {
        click_button: "upload2_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img2",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload2',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper2',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue2 = info.key
	}
	imgKeyValue3 = null

	new FileUploader {
        click_button: "upload3_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img3",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload3',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper3',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue3 = info.key
	}
	imgKeyValue4 = null

	new FileUploader {
        click_button: "upload4_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img4",                   # 上传成功后回显的图片ID
        domain: "http://p99igky61.bkt.clouddn.com/",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload4',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper4',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue4 = info.key
	}

	ClearNullArr = (arr) ->
		_tmpArr = new Array()
		k=0
		for i in arr
			if i != null && i != ''
				_tmpArr[k] = i
				k++

		return _tmpArr

	CPictureValue = () ->
		c_picture1 = null
		c_picture2 = null
		c_picture3 = null
		c_picture4 = null
		if imgKeyValue1 is null || imgKeyValue1 is ''
			c_picture1 = $("#c_pricture1").val()
		else
			c_picture1 = imgKeyValue1
		if imgKeyValue2 is null || imgKeyValue2 is ''
			c_picture2 = $("#c_pricture2").val()
		else
			c_picture2 = imgKeyValue2
		if imgKeyValue3 is null || imgKeyValue3 is ''
			c_picture3 = $("#c_pricture3").val()
		else
			c_picture3 = imgKeyValue3
		if imgKeyValue4 is null || imgKeyValue4 is ''
			c_picture4 = $("#c_pricture4").val()
		else
			c_picture4 = imgKeyValue4

		return picture_arr = new Array(c_picture1,c_picture2,c_picture3,c_picture4)

	$("#update_btn").click (e) ->
		picture_arr = CPictureValue()

		picture_arr = ClearNullArr(picture_arr)

		param=
			id: $("#commodity_id").val()
			c_name: $("#c_name").val()
			c_number: $("#c_number").val()
			c_integration: $("#c_integration").val()
			c_picture: picture_arr
			ct_type_id: $("#ct_type_id").val()

		$.post "/api/admin/commodity/update-commodity",param,(res) ->
			if res.status.errCode is 0
				alert "修改成功"
				window.location.href = '/admin/commodity/index'
			else
				alert res.status.message

	$("#delete1_btn").click (e) ->
		$("#c_pricture1").val(null)
		$("#cn_img1").removeAttr('src')
	$("#delete2_btn").click (e) ->
		$("#c_pricture2").val(null)
		$("#cn_img2").removeAttr('src')
	$("#delete3_btn").click (e) ->
		$("#c_pricture3").val(null)
		$("#cn_img3").removeAttr('src')
	$("#delete4_btn").click (e) ->
		$("#c_pricture4").val(null)
		$("#cn_img4").removeAttr('src')


	$("#cancel_btn").click (e) ->
		window.location.href = "/admin/commodity/index"